var e = require("../../api.js"), t = getApp();

Page({
    data: {
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      tabbar_list:[
        'pages/index/index',
        'pages/cat/cat',
        'pages/posts/index',
        'pages/cart/cart',
        'pages/user/user'
      ]
    },
    onLoad: function(e) {
        t.pageOnLoad(this), "undefined" == typeof my ? this.setData({
            platform: "wx"
        }) : this.setData({
            platform: "my"
        });
    },
    onReady: function() {
        t.pageOnReady(this);
    },
    onShow: function() {
        t.pageOnShow(this);
    },
    onHide: function() {
        t.pageOnHide(this);
    },
    onUnload: function() {
        t.pageOnUnload(this);
    },
    getUserInfo: function(e) {
        var t = this;
        var scene = '';
        wx.getStorage({
          key: 'parent_id',
          success(res) {
            scene = { user_id: res.data}
          },
          fail(){
            wx.getStorage({
              key: 'scene',
              success(res) {
                scene = res.data
              }
            })
          }
        })
        "getUserInfo:ok" == e.detail.errMsg && wx.login({
            success: function(n) {
                var o = n.code;
              if (t.is_login()){
                wx.getStorage({
                  key: 'address_info',
                  success: function (res) {
                    console.log(res);
                    t.unionLogin({
                      code: o,
                      user_info: e.detail.rawData,
                      encrypted_data: e.detail.encryptedData,
                      iv: e.detail.iv,
                      signature: e.detail.signature,
                      parent_id: scene.user_id ? scene.user_id : 0,
                      shop_id: res.data.shop_id ? res.data.shop_id : 0
                    });
                  },
                })
              }
                
            },
            fail: function(e) {}
        });
    },
    back_one:function(){
      var pages = getCurrentPages();
      var url = pages[pages.length - 2] ? pages[pages.length - 2].route : '';
      if (!(url != "pages/cart/cart" && url != 'pages/user/user' && url != 'pages/cat/cat' && url != 'pages/index/index' && url != 'pages/posts/index')) {
        wx.switchTab({
          url: '/'+url
        });
      }else{
        if (pages.length < 3) {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }else{
          url = pages[pages.length - 3] ? pages[pages.length - 3].route : 'pages/index/index';
          if (!(url != "pages/cart/cart" && url != 'pages/user/user' && url != 'pages/cat/cat' && url != 'pages/index/index' && url != 'pages/posts/index')) {
            wx.switchTab({
              url: '/' + url
            });
          } else {
            wx.redirectTo({
              url: '/' + url,
            })
          }
        }
      }
    },
    myLogin: function() {
        var e = this;
        "undefined" != typeof my && my.getAuthCode({
            scopes: "auth_user",
            success: function(t) {
              if(e.is_login()){
                var r = wx.getStorageSync("address_info");
                e.unionLogin({
                  code: t.authCode,
                  shop_id: r.shop_id
                });
              }
            }
        });
    },
    is_login:function(tt){
      var that = this;
      wx.getStorage({
        key: 'address_info',
        success: function (res) {
          return true;
        },
        fail: function () {
          wx.getLocation({
            success: function (res) {
              that.setData({
                longitude: res.longitude,
                latitude: res.latitude
              });
              getApp().request({
                url: e.default.index_position,
                data: {
                  longitude: res.longitude,
                  latitude: res.latitude,
                  tz_id: 0,
                },
                method: "POST",
                success: function (t) {
                    wx.setStorage({
                      key: "address_info",
                      data: {
                        nickname: t.nickname,
                        name: t.name,
                        address: t.address,
                        distance: t.distance,
                        logo: t.logo,
                        shop_id: t.shop_id
                      }
                    })
                    return true;
                },
              })

            },
            complete: function () {
              return true;
            }
          });
        }
      }) 
      return true;
    },

    unionLogin: function(t) {
      var that =this;
      // console.log(t);
       t.user_info = this.unicode(t.user_info);
      // console.log(t);
        wx.showLoading({
            title: "正在登录",
            mask: !0
        }), getApp().request({
            url: e.passport.login,
            method: "POST",
            data: t,
            success: function(e) {
                if (0 == e.code) {
                    wx.setStorageSync("access_token", e.data.access_token), wx.setStorageSync("user_info", e.data);
                    var t = wx.getStorageSync("login_pre_page");
                    t && t.route || wx.switchTab({
                        url: "/pages/index/index"
                    });
                    var n = 0;
                    (n = t.options && t.options.user_id ? t.options.user_id : t.options && t.options.scene ? t.options.scene : wx.getStorageSync("parent_id")) && 0 != n && getApp().bindParent({
                        parent_id: n
                    }) , that.tabbar_to(t);
                      
                } else wx.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    unicode: function (str) {
      var unid = '\\u';
      for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i).toString(10) < 16) {
          unid += '000' + str.charCodeAt(i).toString(16);
        } else if (str.charCodeAt(i).toString(10) < 256) {
          unid += '00' + str.charCodeAt(i).toString(16);
        } else if (str.charCodeAt(i).toString(10) < 4096) {
          unid += '0' + str.charCodeAt(i).toString(16);
        } else {
          unid += str.charCodeAt(i).toString(16);
        }
        if (i < str.length - 1) {
          unid += '\\u'
        }

      }
      return unid;
    },
    tabbar_to:function(t){
      var that = this;
      var istabber = 0;
      for (var i = 0; i < that.data.tabbar_list.length; i++) {
        console.log(that.data.tabbar_list[i]);
        if (t.route == that.data.tabbar_list[i]) {
          istabber = 1;
        }
      }
      if (istabber == 1) {
        wx.switchTab({
          url: "/" + t.route + "?" + getApp().utils.objectToUrlParams(t.options)
        });
      } else {
        wx.redirectTo({
          url: "/" + t.route + "?" + getApp().utils.objectToUrlParams(t.options)
        });
      }
    }
});